<template>
  <div class="department-container">
    <div class="app-container">
      <el-card>
          <!-- 用一个行列布局 -->
          <el-row type="flex" justify="space-between" align="middle" style="height: 40px">
            <el-col :span="20">
              <span>江苏传智播客教育科技股份有限公司</span>
            </el-col>
            <el-col :span="4">
              <el-row type="flex" justify="end">
                <!-- 两个内容 -->
                <el-col>负责人</el-col>
                <el-col>操作</el-col>
              </el-row>
            </el-col>
          </el-row>
          <el-tree
            :data="list"
            :props="defaultProps"
            :default-expand-all="true"
          >
            <!-- 作用域插槽 data拿到的是每一个子节点的对象 -->
            <template #default="{ data }">
              <el-row
                type="flex"
                justify="space-between"
                align="middle"
                style="height: 40px; width: 100%;"
              >
                <el-col :span="20">
                  <span>{{ data.name }}</span>
                </el-col>
                <el-col :span="4">
                  <el-row type="flex" justify="end">
                    <!-- 两个内容 -->
                    <el-col>{{ data.manager }}</el-col>
                    <el-col>
                      <!-- 下拉菜单 element -->
                      <el-dropdown>
                        <span> 操作<i class="el-icon-arrow-down" /> </span>
                        <el-dropdown-menu slot="dropdown">
                          <el-dropdown-item @click.native="addDepartment(data.id)">添加子部门</el-dropdown-item>
                          <el-dropdown-item @click.native="editDepartment(data.id)">编辑部门</el-dropdown-item>
                          <el-dropdown-item @click.native="remov(data.id)">删除部门</el-dropdown-item>
                        </el-dropdown-menu>
                      </el-dropdown>
                    </el-col>
                  </el-row>
                </el-col>
              </el-row>
            </template>
          </el-tree>
      </el-card>   
    </div>
    <add-department
    ref="addDepart"
    :show-dialog="dialogVisible"
    :cur-id="curId"
    @close-dialog="closeDialog"
    @reset-data="resetDate"
    />
  </div>
</template>

<script>
import { getDepartments, delDepartments } from '@/api/department'
import transTree from '@/utils/transTree'
import addDepartment from './components/add-department.vue' 
export default ({
  components: {
    addDepartment
  },
  data() {
    return {
      list: [{
        name: '财务部',
        children: [
          {
            name: '财务核算部'
          },
          {
            name: '税务核算部'
          }
        ]
      }],
      defaultProps: {
        label: 'name',
        children: 'children'
      },
      dialogVisible: false,
      curId: ''
    }
  },
  methods: {
    async fetchDepartment() {
      const { depts } = await getDepartments()
      this.list = transTree(depts)
    },
    addDepartment(id) {
      this.dialogVisible = true
      console.log(id)
      this.curId = id
    },
    closeDialog() {
      this.dialogVisible = false
    },
    // 使用父子通信的方法
    resetDate() {
      this.fetchDepartment()
      this.curId = ''
    },
    // 删除功能
    remov(id) {
      this.$confirm('您确定进行删除么, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        await delDepartments(id)
        this.$message.success('删除部门成功')
        this.fetchDepartment()
      }).catch(() => {})
    },
    // 编辑功能
    editDepartment(id) {
      this.dialogVisible = true
      this.$refs.addDepart.getDetail(id)
    }
  },
  mounted() {
    this.fetchDepartment()
  }
})
</script>
